package com.sks.web.action.privilege;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.stereotype.Controller;

import com.sks.bean.PageView;
import com.sks.bean.pojo.Favorites;
import com.sks.service.house.FavoritesService;
import com.sks.service.privilege.EmployeeService;
import com.sks.service.privilege.UsersService;
import com.sks.utils.WebUtil;
import com.sks.web.formbean.privilege.EmployeeForm;
import com.sks.web.formbean.privilege.UsersForm;
/**
 * 外部用户登录
 */
@Controller("/sks/privilege/users/logon")
public class UsersLogonAction extends Action {
	@Resource UsersService usersService;

	@Resource(name = "favoritesServiceBean") private FavoritesService favoritiesService;
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UsersForm formbean = (UsersForm)form;
		if(formbean.getLoginName()!=null && !"".equals(formbean.getLoginName().trim())
				&& formbean.getLoginPwd()!=null && !"".equals(formbean.getLoginPwd().trim())){
			if(usersService.validate(formbean.getLoginName().trim(), formbean.getLoginPwd().trim())){
				request.getSession().setAttribute("users", usersService.findByLoginName(formbean.getLoginName().trim()));
				common(request);
				String url = "/sks/frontpages/privilege/deployment/list.do";
				if(formbean.getDirectUrl()!=null && !("").equals(formbean.getDirectUrl())) 
					url = formbean.getDirectUrl();
				request.setAttribute("directUrl", url);
				return mapping.findForward("directUrl");
			}else{
				request.setAttribute("message", "用户名或密码有误");
			}
		}
		return mapping.findForward("logon");
	}//1>显示登录界面(当用户没有提供用户名及密码的时候) 2>实现用户名及密码的校验
	
	private void common (HttpServletRequest request){
		
		StringBuffer build = new StringBuffer(" o.user.userId = '" + WebUtil.getUsers(request).getUserId() + "'");
		build.append(" and o.building.buildingId is not null");
		PageView<Favorites> pv = new PageView<Favorites>(9999999, 0);//9999999是显示的最大结果
	    pv.setQueryResult(favoritiesService.getScrollData(0, pv.getMaxresult(), build.toString(),null , null));	    
	    request.setAttribute("buildNum", pv.getTotalrecord());  
	    request.setAttribute("pv", pv);  
	    
	    
	    //获取房源
	    StringBuffer house = new StringBuffer(" o.user.userId = '" + WebUtil.getUsers(request).getUserId() + "'");
	    house.append(" and o.house.houseId is not null");
		PageView<Favorites> page = new PageView<Favorites>(9999999, 0);//9999999是显示的最大结果
		page.setQueryResult(favoritiesService.getScrollData(0, pv.getMaxresult(), house.toString(),null , null)); 
		request.setAttribute("houseNum", page.getTotalrecord());
		request.setAttribute("page", page);
	}
}
